﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqModel.Data;
using System.IO;


public partial class Add_ProductImage : System.Web.UI.Page
{
    Product product = new Product();
    List<ProductImageInfo> listimg = new List<ProductImageInfo>();
    IDataRepository _dataObject = new DataRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        product = Session["Product"] as Product;
        if (!IsPostBack)
        {
            BindProductImages();
        }
    }

    #region OnUploadComplete  

    protected void File_Upload(object sender, AjaxFileUploadEventArgs e)
    {
        #region add uploded file in to list
        listimg = Session["ProductImage"] as List<ProductImageInfo>;
        if (listimg == null)
            listimg = new List<ProductImageInfo>();

        string filename = "PIMG_" + System.Guid.NewGuid() + "_" + e.FileName;
        string strDestPath = Server.MapPath("~/ProductImages/");
        if (!Directory.Exists(strDestPath))
            Directory.CreateDirectory(strDestPath);

        AjaxFileUpload1.SaveAs(@strDestPath + filename);
        ProductImageInfo pimg = new ProductImageInfo();
        pimg = new ProductImageInfo(_dataObject);
        pimg.ImageName = filename;
        pimg.CreatedDate = DateTime.Now;
        pimg.IsDeleted = false;
        pimg.ProductId = product.Id;
      //  pimg.Create();
        listimg.Add(pimg);
        Session["ProductImage"] = listimg;
        #endregion
    }
    #endregion

    #region save product and product image info
    protected void OnSave(object sender, EventArgs e)
    {
        product = new Product(_dataObject);
        product = Session["Product"] as Product;
        listimg = Session["ProductImage"] as List<ProductImageInfo>;

        #region display error msg

        if (product.Id <= 0)
        {
            if (listimg == null)
            {
                divMessage.InnerHtml = "<strong>Error!</strong>Please Upload Product Images..";
                divMessage.Attributes.Add("class", "alert alert-danger");
                return;
            }
        }
        #endregion

        //save ProductImage Info in update mode
        string strDestPath = Server.MapPath("~/ProductIamges/");
        if (listimg != null && listimg.Count > 0 && product.Id > 0)
        {
            ProductImageInfo productImage = new ProductImageInfo();
            productImage = new ProductImageInfo(_dataObject);
            productImage.CreateAll(listimg);
        }

        #region save product info

        if (product.Id <= 0)
        {
            //enter Product Info
            //ProductImageInfo productImage = new ProductImageInfo();
            //productImage = new ProductImageInfo(_dataObject);
            if (listimg != null && listimg.Count > 0)
                product.ProductImageInfos.AddRange(listimg);
            product.Create();
        }
        #endregion

        #region save product image info
       
        Response.Redirect("~/Product-List.aspx");
        #endregion
    }
    #endregion

    #region BindData
    private void BindProductImages()
    {
        ProductImageInfo productImage = new ProductImageInfo();
        List<ProductImageInfo> productImages = new List<ProductImageInfo>();
        productImage = new ProductImageInfo(_dataObject);
        productImage.ProductId = product.Id;
        productImages = productImage.GetImagesById();
        //rptProductImage.DataSource = productImages;
        //rptProductImage.DataBind();
        dlProductImages.DataSource = productImages;
        dlProductImages.DataBind();
    }
    #endregion

    #region itemcommand
    protected void ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            ProductImageInfo productImage = new ProductImageInfo();
            productImage = new ProductImageInfo(_dataObject);
            productImage.Id = Convert.ToInt32(e.CommandArgument);
            productImage.Delete();
            BindProductImages();

        }
    }
    //protected void save(object sender, EventArgs e)
    //{
    //    BindProductImages();
    //}
    #endregion

}
